package com.bing.bingPro.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bing.bingPro.controller.dto.TZwMeteringVelocityDto;
import com.bing.bingPro.controller.vo.TBorderLatestStatsVo;
import com.bing.bingPro.controller.vo.TBorderMeteringVo;
import com.bing.bingPro.entity.TBworderLstestNqDetails;
import com.bing.bingPro.entity.TBworderLstestStats;
import com.bing.bingPro.enums.TZwMeteringVelocityEnums;
import com.bing.bingPro.enums.TimeGranularityEnums;
import com.bing.bingPro.service.TBorderLatestNqDetailsService;
import com.bing.bingPro.mapper.TBworderLstestNqDetailsMapper;
import com.bing.bingPro.util.ProvinceUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static org.apache.commons.lang3.StringUtils.isNotEmpty;

/**
* @author wangqilong
* @description 针对表【t_bworder_lstest_nq_details】的数据库操作Service实现
*/
@Service
public class TBorderLatestNqDetailsServiceImpl extends ServiceImpl<TBworderLstestNqDetailsMapper, TBworderLstestNqDetails>
    implements TBorderLatestNqDetailsService {


    @Override
    public List<TBorderMeteringVo> tBorderMeteringList(TZwMeteringVelocityDto tZwMeteringVelocityDto) {
        String replace = tZwMeteringVelocityDto.getTime().replace("-", "");
        String province = tZwMeteringVelocityDto.getProvince();
        String orderType = ProvinceUtil.getOrderType(tZwMeteringVelocityDto.getOrderType());
        List<TBworderLstestNqDetails> statsList;

        if (Objects.equals(TZwMeteringVelocityEnums.METERING.getCode(), tZwMeteringVelocityDto.getIdentify())) {
            statsList = queryDetails(replace, orderType, province, Arrays.asList("1", "3"));
        } else {
            statsList = queryDetails(replace, orderType, province, Arrays.asList("2", "3"));
        }

        return statsList.stream()
                .map(tBorderLatestStats -> mapToTBorderLatestStatsVo(tBorderLatestStats, tZwMeteringVelocityDto.getTime()))
                .collect(Collectors.toList());
    }

    @Override
    public IPage<TBorderMeteringVo> tBorderMeteringPageList(TZwMeteringVelocityDto tZwMeteringVelocityDto) {
        String replace = tZwMeteringVelocityDto.getTime().replace("-", "");
        String province = tZwMeteringVelocityDto.getProvince();
        String orderType = ProvinceUtil.getOrderType(tZwMeteringVelocityDto.getOrderType());
        List<String> notQualifiedTypes = new ArrayList<>();
        IPage<TBorderMeteringVo> voIPage = new Page<>();
        if (Objects.equals(TZwMeteringVelocityEnums.METERING.getCode(), tZwMeteringVelocityDto.getIdentify())) {
            notQualifiedTypes.addAll(Arrays.asList("1", "3"));
        } else {
            notQualifiedTypes.addAll(Arrays.asList("2", "3"));
        }

        Page<TBworderLstestNqDetails> page = new Page<>(tZwMeteringVelocityDto.getPageNum(), tZwMeteringVelocityDto.getPageSize());

        IPage<TBworderLstestNqDetails> pageResult = baseMapper.selectPage(page, Wrappers.lambdaQuery(TBworderLstestNqDetails.class)
                .eq(isNotEmpty(replace), TBworderLstestNqDetails::getDataTime, replace)
                .eq(isNotEmpty(orderType), TBworderLstestNqDetails::getOrderType, orderType)
                .eq(isNotEmpty(province), TBworderLstestNqDetails::getProvinceName, province)
                .in(TBworderLstestNqDetails::getNotQualifiedType, notQualifiedTypes));

        List<TBorderMeteringVo> result = pageResult.getRecords().stream()
                .map(tBorderLatestStats -> mapToTBorderLatestStatsVo(tBorderLatestStats, tZwMeteringVelocityDto.getTime()))
                .collect(Collectors.toList());
        voIPage.setRecords(result);
        voIPage.setTotal(pageResult.getTotal());
        voIPage.setPages(pageResult.getPages());
        voIPage.setCurrent(pageResult.getCurrent());
        voIPage.setSize(pageResult.getSize());
        return voIPage;

    }

    private List<TBworderLstestNqDetails> queryDetails(String replace, String orderType, String province, List<String> notQualifiedTypes) {
        return baseMapper.selectList(Wrappers.lambdaQuery(TBworderLstestNqDetails.class)
                .eq(isNotEmpty(replace), TBworderLstestNqDetails::getDataTime, replace)
                .eq(isNotEmpty(orderType), TBworderLstestNqDetails::getOrderType, orderType)
                .eq(isNotEmpty(province), TBworderLstestNqDetails::getProvinceName, province)
                .in(TBworderLstestNqDetails::getNotQualifiedType, notQualifiedTypes));
    }

    private TBorderMeteringVo mapToTBorderLatestStatsVo(TBworderLstestNqDetails tBorderLatestStats, String time) {
        TBorderMeteringVo vo = new TBorderMeteringVo();
        BeanUtils.copyProperties(tBorderLatestStats, vo);

        String orderType = tBorderLatestStats.getOrderType();
        switch (orderType) {
            case "cp":
                vo.setOrderType("投诉工单");
                break;
            case "install":
                vo.setOrderType("装机工单");
                break;
            default:
                break;
        }

        vo.setDataTime(time);
        return vo;
    }
}




